"""Admin for emencia.news"""
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from emg_intra.news.models import Category
from emg_intra.news.models import News
from emg_intra.news.models import Document


class NewsAdmin(admin.ModelAdmin):
    list_display = ('title', 'tags', 'author', 'visibility',
                    'language')
    date_hierarchy = 'creation'
    prepopulated_fields = {'slug': ('title',)}
    list_filter = ('author', 'visibility', 'creation')
    search_fields = ('title', 'slug', 'description', 'content', 'tags')
    filter_horizontal = ('relations', 'documents')
    fieldsets = ((None, {'fields': ('title', 'description', 'content')}),
                 (_('Attributs'), {'fields': ('language', 'category', 'tags',
                                              'documents', 'relations')}),
                 (_('Metadata'), {'fields': ('slug', 'visibility', 'ordering',
                                             'publication_start', 'publication_end')}),
                 )

    def save_model(self, request, news, form, change):
        if not news.pk:
            news.author = request.user
        news.save()

admin.site.register(News, NewsAdmin)


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug', 'description')
    search_fields = ('title', 'slug', 'description')
    prepopulated_fields = {'slug': ('title',)}

admin.site.register(Category, CategoryAdmin)


class DocumentAdmin(admin.ModelAdmin):
    list_display = ('title', 'document')
    search_fields = ('title', 'description')

admin.site.register(Document, DocumentAdmin)
